unit uConsultaDescontoComissao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ExtCtrls, DB, DbTables,
  Formulario_Padrao_Consulta, TFlatButtonUnit, StdCtrls, Buttons, DBClient, Grids, DBGrids, Dialogs,
  Mask, ToolEdit, CurrEdit;

type
  TfrmConsultaDescontoComissao = class(TFrmConsulta)
    dbgConsulta: TDBGrid;
    Label1: TLabel;
    cdsConsulta: TClientDataSet;
    dsConsulta: TDataSource;
    cdsConsultaCodigo: TStringField;
    cdsConsultaComissao: TFloatField;
    edtComissao: TCurrencyEdit;
    btnGravar: TFlatButton;
    cdsConsultaDescontos: TStringField;
    procedure dbgConsultaTitleClick(Column: TColumn);
    procedure BtnFecharClick(Sender: TObject);
    procedure dbgConsultaDblClick(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure edtComissaoExit(Sender: TObject);

  private
    { Private declarations }
    procedure FazConsulta;
    function RetornaDescontos(vStrCodigo:String):String;

  public
    { Public declarations }
    Codigo:String;
    BlnCadastrando:boolean;

  end;

var
  frmConsultaDescontoComissao: TfrmConsultaDescontoComissao;

implementation

uses
  Biblio, DM, uCadastroDescontoComissao;

{$R *.dfm}

{ TfrmConsultaDescontoComissao }

procedure TfrmConsultaDescontoComissao.FazConsulta;
var
  qryConsulta:TQuery;
  strSQL:String;

begin
  qryConsulta := TQuery.Create(DmInt.Db);
  qryConsulta.DatabaseName := DmInt.Db.DatabaseName;
  qryConsulta.Active := false;

  try
    if (edtComissao.Value = 0) then begin
      strSQL := 'select * from descontocomissao ';

    end else begin
      strSQL := 'select * ' +

                'from descontocomissao ' +

                'where comissao >= ' + formata_valor(edtComissao.Value);

    end;

    cdsConsulta.Close;
    cdsConsulta.CreateDataSet;
    cdsConsulta.Open;

    qryConsulta.Close;
    qryConsulta.SQL.Clear;
    qryConsulta.Params.Clear;
    qryConsulta.SQL.Add(strSQL);
    qryConsulta.Open;

    while not(qryConsulta.Eof) do begin
      cdsConsulta.Append;
      cdsConsultaCodigo.AsString := qryConsulta.FieldByName('Codigo').AsString;
      cdsConsultaComissao.AsFloat := qryConsulta.FieldByName('Comissao').AsFloat;
      cdsConsultaDescontos.AsString := RetornaDescontos(qryConsulta.FieldByName('Codigo').AsString);
      cdsConsulta.Post;

      qryConsulta.Next;

    end;

  finally
    qryConsulta.Close;
    FreeAndNil(qryConsulta);

  end;

end;

procedure TfrmConsultaDescontoComissao.dbgConsultaTitleClick(Column: TColumn);
begin
  inherited;
  cdsConsulta.IndexFieldNames := Column.FieldName;
end;

procedure TfrmConsultaDescontoComissao.BtnFecharClick(Sender: TObject);
begin
  Self.Codigo := cdsConsultaCodigo.AsString;
  inherited;
end;

procedure TfrmConsultaDescontoComissao.dbgConsultaDblClick(Sender: TObject);
begin
  inherited;
  BtnFecharClick(Sender);
end;

procedure TfrmConsultaDescontoComissao.btnGravarClick(Sender: TObject);
var
  frm:TfrmCadastroDescontoComissao;

begin
  inherited;
  frm := TfrmCadastroDescontoComissao.Create(Self);

  try
    frm.ShowModal;
  finally
    FreeAndNil(frm);

  end;

end;

procedure TfrmConsultaDescontoComissao.FormActivate(Sender: TObject);
begin
  inherited;
  if (Self.BlnCadastrando) then begin
    btnGravar.Visible := false;
  end else begin
    btnGravar.Visible := true;
  end;

  FazConsulta;

end;

function TfrmConsultaDescontoComissao.RetornaDescontos(vStrCodigo: String): String;
var
  qryConsultaDesconto:TQuery;
  strSQL:String;

begin
  qryConsultaDesconto := TQuery.Create(DmInt.Db);
  qryConsultaDesconto.DatabaseName := DmInt.Db.DatabaseName;
  qryConsultaDesconto.Active := false;

  try
    strSQL := 'select desconto from itemdescontocomissao where codigo = ' + QuotedStr(vStrCodigo);

    qryConsultaDesconto.Close;
    qryConsultaDesconto.SQL.Clear;
    qryConsultaDesconto.Params.Clear;
    qryConsultaDesconto.SQL.Add(strSQL);
    qryConsultaDesconto.Open;

    Result := '';
    while not(qryConsultaDesconto.Eof) do begin
      if (Result <> '') then begin
        Result := Result + ' - ';
      end;

      Result := Result + FloattoStrF(qryConsultaDesconto.FieldByName('desconto').AsFloat,
                                                                                   ffNumber, 15, 2);

      qryConsultaDesconto.Next;

    end;

  finally
    qryConsultaDesconto.Close;
    FreeAndNil(qryConsultaDesconto);

  end;

end;

procedure TfrmConsultaDescontoComissao.edtComissaoExit(Sender: TObject);
begin
  inherited;
  FazConsulta;
end;

end.
